import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { ApiBearerAuth, ApiOkResponse, ApiTags, ApiOperation } from '@nestjs/swagger';
import { Roles, RolesDocument } from 'src/db/schemas/roles.schema';
import { DeleteDto } from 'src/dto/delete.dto';
import { Model } from 'mongoose';
import { ResponseBodyDto } from '../../dto';
import { CreateAndUpdateRoleDto } from './dto/CreateAndUpdateRoleDto';
import { QueryRolesDto } from './dto/QueryRolesDto';
import { ReturnAllRolesDto, ReturnRolesPaginationDto } from './dto/ReturnRolesPaginationDto';
import { UpdateRoleOffDto } from './dto/UpdateRoleOffDto';
import { RolesService } from './roles.service';
import { getAll, getOneById, updateOneById } from 'src/utils';
import { UsersDto } from 'src/db/dto';
import { RoleDto } from 'src/db/dto/role.dto';

@ApiTags('角色')
@Controller('roles')
@ApiBearerAuth('Authorization')
export class RolesController {
  constructor(
    private readonly rolesService: RolesService,
    @InjectModel(Roles.name) private rolesModel: Model<RolesDocument>,
  ) { }

  @Post('getRolesPagination')
  @ApiOkResponse({
    description: '分页数据',
    type: ReturnRolesPaginationDto,
  })
  getRolePagination(@Body() queryRolesDto: QueryRolesDto) {
    return this.rolesService.getRolePagination(queryRolesDto);
  }

  @Post('getAllRoles')
  @ApiOkResponse({
    description: '所有数据',
    type: ReturnAllRolesDto,
  })
  @ApiOperation({
    summary: '根据条件获取所有数据',
  })
  getAllWebsiteTypesByCondition(@Body() roleDto: RoleDto) {
    return getAll(
      this.rolesModel,
      roleDto
    );
  }

  @Post('addAndUpdateRole')
  @ApiOkResponse({
    description: '操作结果',
    type: ResponseBodyDto,
  })
  addAndUpdateRole(@Body() createAndUpdateRoleDto: CreateAndUpdateRoleDto) {
    return this.rolesService.addAnyUpdateRole(createAndUpdateRoleDto);
  }

  @Delete('deleteRoles')
  @ApiOkResponse({
    description: '删除结果',
    type: ResponseBodyDto,
  })
  deleteRoles(@Body() deleteDto: DeleteDto) {
    return this.rolesService.deleteRoles(deleteDto);
  }

  @Put('setRoleOffById/:_id')
  @ApiOkResponse({
    description: '更新结果',
    type: ResponseBodyDto,
  })
  setRoleOffById(
    @Param('_id') _id: string,
    @Body() updateRoleOffDto: UpdateRoleOffDto,
  ) {
    return updateOneById(this.rolesModel, _id, updateRoleOffDto);
  }

  @Get('getRoleById/:_id')
  @ApiOkResponse({
    description: '获取数据结果',
    type: ResponseBodyDto,
  })
  getRoleById(@Param('_id') _id: string) {
    return getOneById(this.rolesModel, _id);
  }
}
